﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SmartlForm.SimpleCrawler.Model
{
    /// <summary>
    /// 站点
    /// </summary>
    [XmlRoot("Site")]
    [Serializable]
    public class SiteModel
    {
        public long Id { get; set; }

        /// <summary>
        /// 请求延时
        /// </summary>
        [XmlAttribute("delay")]
        public int Delay { get; set; }

        /// <summary>
        /// 调度间隔
        /// </summary>
        [XmlAttribute("schedule")]
        public long Schedule { get; set; }


        /// <summary>
        /// 超时时间
        /// </summary>
        [XmlAttribute("timeout")]
        public int Timeout { get; set; }

        /// <summary>
        /// 站点地址
        /// </summary>
        [XmlAttribute("url")]
        public string Url { get; set; }

        /// <summary>
        /// 站点编码
        /// </summary>
        [XmlAttribute("encoding")]
        public string Encoding { get; set; }

        /// <summary>
        /// 抓取目标配置信息
        /// </summary>
        public TargetsModel Targets { get; set; }

        public long accountId { get; set; }

        /// <summary>
        /// 关键词类型
        /// </summary>
        public string Type { get; set; }
    }

    public class TargetsModel
    {
        [XmlArrayItem("Entity")]
        public List<EntityModel> Entitys { get; set; }

        [XmlArrayItem("Rule")]
        public List<SourceRulesModel> SourceRules { get; set; }
    }


    /// <summary>
    /// 数据源规则
    /// </summary>
    public class SourceRulesModel
    {
        [XmlElementAttribute("DigUrls", IsNullable = false)]
        public List<DigUrlsModel> Rule { get; set; }
    }


    public class DigUrlsModel
    {
        [XmlElementAttribute("Field", IsNullable = false)]
        public List<FieldModel> Field { get; set; }
    }

    public class EntityModel
    {

        [XmlElementAttribute("Field", IsNullable = false)]
        public List<FieldModel> Field { get; set; }

        [XmlElementAttribute("Array", IsNullable = false)]
        public ArrayModel Array { get; set; }
    }


    public class ArrayModel
    {
        [XmlAttribute("xpath")]
        public string XPath { get; set; }

        [XmlAttribute("name")]
        public string Name { get; set; }

        [XmlElementAttribute("Field", IsNullable = false)]
        public List<FieldModel> Field { get; set; }
    }

    public class FieldModel
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        [XmlAttribute("name")]
        public string Name { get; set; }
        /// <summary>
        /// 是否去掉前后空格
        /// </summary>
        [XmlAttribute("isTrim")]
        public bool IsTrim { get; set; }

        /// <summary>
        /// 是否去掉前后空格
        /// </summary>
        [XmlAttribute("isDelHtml")]
        public bool IsDelHtml { get; set; }

        /// <summary>
        /// 解析器
        /// </summary>
        [XmlArrayItem("Parser")]
        public List<ParserModel> Parsers { get; set; }
    }
    /// <summary>
    /// 解析器
    /// </summary>
    public class ParserModel
    {
        [XmlAttribute("xpath")]
        public string XPath { get; set; }

        /// <summary>
        /// 如果需要取值得属性名称
        /// </summary>
        [XmlAttribute("attribute")]
        public string Attribute { get; set; }

        /// <summary>
        /// 正则表达式
        /// </summary>
        [XmlAttribute("exp")]
        public string Exp { get; set; }

        /// <summary>
        /// 正则表达式
        /// </summary>
        [XmlAttribute("expIndex")]
        public string ExpIndex { get; set; }


        /// <summary>
        /// 格式化
        /// </summary>
        [XmlAttribute("format")]
        public string Format { get; set; }
    }

}
